package com.infocollect.project.grant.controller;

import java.util.ArrayList;
import java.util.List;

import com.infocollect.project.grant.domain.GrFamiliesExampoint;
import com.infocollect.project.grant.domain.GrFamiliesExampointDto;
import com.infocollect.project.grant.service.IGrFamiliesExampointService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.infocollect.framework.aspectj.lang.annotation.Log;
import com.infocollect.framework.aspectj.lang.enums.BusinessType;
import com.infocollect.project.grant.domain.GrExamPoint;
import com.infocollect.project.grant.service.IGrExamPointService;
import com.infocollect.framework.web.controller.BaseController;
import com.infocollect.framework.web.domain.AjaxResult;
import com.infocollect.common.utils.poi.ExcelUtil;
import com.infocollect.framework.web.page.TableDataInfo;

/**
 * 考点管理Controller
 *
 * @author infocollect
 * @date 2022-05-14
 */
@RestController
@RequestMapping("/grant/exampoint")
public class GrExamPointController extends BaseController
{
    @Autowired
    private IGrExamPointService grExamPointService;

    @Autowired
    private IGrFamiliesExampointService grFamiliesExampointService;
    @Autowired
    private IGrExamPointService examPointService;
    /**
     * 查询考点管理列表
     */
    @PreAuthorize("@ss.hasPermi('grant:exampoint:list')")
    @GetMapping("/list")
    public TableDataInfo list(GrExamPoint grExamPoint)
    {
        startPage();
        List<GrExamPoint> list = grExamPointService.selectGrExamPointList(grExamPoint);
        return getDataTable(list);
    }

    /**
     * 导出考点管理列表
     */
    @PreAuthorize("@ss.hasPermi('grant:exampoint:export')")
    @Log(title = "考点管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GrExamPoint grExamPoint)
    {
        List<GrExamPoint> list = grExamPointService.selectGrExamPointList(grExamPoint);
        ExcelUtil<GrExamPoint> util = new ExcelUtil<GrExamPoint>(GrExamPoint.class);
        return util.exportExcel(list, "exampoint");
    }

    /**
     * 获取考点管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('grant:exampoint:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(grExamPointService.selectGrExamPointById(id));
    }

    /**
     * 新增考点管理
     */
    @PreAuthorize("@ss.hasPermi('grant:exampoint:add')")
    @Log(title = "考点管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GrExamPoint grExamPoint)
    {
        return toAjax(grExamPointService.insertGrExamPoint(grExamPoint));
    }

    /**
     * 修改考点管理
     */
    @PreAuthorize("@ss.hasPermi('grant:exampoint:edit')")
    @Log(title = "考点管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GrExamPoint grExamPoint)
    {
        return toAjax(grExamPointService.updateGrExamPoint(grExamPoint));
    }

    /**
     * 删除考点管理
     */
    @PreAuthorize("@ss.hasPermi('grant:exampoint:remove')")
    @Log(title = "考点管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(grExamPointService.deleteGrExamPointByIds(ids));
    }
    @GetMapping("/getExamPointlist")
    public AjaxResult getExamPointlist(GrExamPoint grExamPoint)
    {
        List<GrExamPoint> list = grExamPointService.selectGrExamPointList(grExamPoint);
        return AjaxResult.success(list);
    }

    /**
     * 根据选择的科类关联出考点选择列表
     * @param grFamiliesExampointDto
     * @return
     */
    @ResponseBody
    @PostMapping("getExamponitListByFamiliesId")
    public AjaxResult getExamponitListByFamiliesId(@RequestBody GrFamiliesExampointDto grFamiliesExampointDto){
        GrFamiliesExampoint familiesExampoint = new GrFamiliesExampoint();
        familiesExampoint.setGrantId(grFamiliesExampointDto.getGrantId());
        familiesExampoint.setFamiliesId(grFamiliesExampointDto.getFamiliesId());
        List<GrFamiliesExampoint> familiesExampointList = grFamiliesExampointService.queryGrfamiliesExampointList(familiesExampoint);
        List<Long> examPointIds = new ArrayList<>();
        familiesExampointList.stream().forEach(familiesExampointIns->{
            examPointIds.add(familiesExampointIns.getExamPointId());
        });
        List<GrExamPoint> examPointList = examPointService.selectGrExamPointByIds(examPointIds);
        return AjaxResult.success(examPointList);
    }
}
